﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Net;
using Shell32;

namespace AnaViewer
{
    /// <summary>
    /// 常量定义
    /// </summary>
    public class Consts
    {
        /// <summary>
        /// 公司网址
        /// </summary>
        internal const string Website = "http://chuangen.name/";
        /// <summary>
        /// 联系我们的Email地址。
        /// </summary>
        internal const string Email = "chuangen@126.com";

        internal const string HelpFile = "AnaViewer.chm";
    }

    public partial class Common
    {
        /// <summary>
        /// 获得带圆角的形状。
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="r">圆角的半径</param>
        /// <returns></returns>
        public static GraphicsPath GetPath(Rectangle rect, int r)
        {
            GraphicsPath path = new GraphicsPath(FillMode.Winding);
            //圆角的直径
            int R = r * 2;
            path.AddArc(new Rectangle(0, 0, R, R), 180, 90);//左上角
            path.AddLine(new Point(r, 0), new Point(rect.Width - r, 0));
            path.AddArc(new Rectangle(rect.Width - R, 0, R, R), 270, 90);//右上角
            path.AddLine(new Point(rect.Width, 0 + r), new Point(rect.Width, rect.Height - r));
            path.AddArc(new Rectangle(rect.Width - R, rect.Height - R, R, R), 0, 90);//右下角
            path.AddLine(new Point(rect.Width - r, rect.Height), new Point(r, rect.Height));
            path.AddArc(new Rectangle(0, rect.Height - R, R, R), 90, 90);//左下角
            path.AddLine(new Point(0, rect.Height - r), new Point(0, r));
            path.CloseFigure();

            using (Matrix matrix = new Matrix())
            {
                matrix.Translate(rect.X, rect.Y, MatrixOrder.Append);
                path.Transform(matrix);
            }
            return path;
        }


        public static Image GetImage(string url)
        {//创建一个HTTP请求 
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            //获取代理服务器的回应 
            HttpWebResponse res = null;
            try
            {
                res = (HttpWebResponse)req.GetResponse();
            }
            catch
            {
                res = null;
            }
            //判断HTTP响应状态 
            if (res == null || res.StatusCode != HttpStatusCode.OK)
            {//访问失败！
                if(res!=null)
                    res.Close();
                return null;
            }
            else
            {
                //获取应答流 
                System.IO.Stream stream = res.GetResponseStream();
                //直接加载到图片（可以直接显示到图片控件） 
                System.Drawing.Bitmap bp = new Bitmap(stream);
                //关闭应答流和服务器的应答 
                stream.Close();
                res.Close();
                return bp;
            }
        }



        public static void GoUrl(string url)
        {//访问网站
            try
            {
                System.Diagnostics.Process.Start(url);
            }
            catch
            {
                //无法打开网站。
            }
        }

        /// <summary>
        /// 显示桌面。动作：先显示桌面，再最小化所有窗口
        /// </summary>
        public static void ToggleDesktop()
        {
            ShellClass sc = new Shell32.ShellClass();
            //sc.MinimizeAll();     //   Win+M   
            //sc.UnminimizeAll();   //   Shift+Win+M   

            IShellDispatch4 sd4 = (IShellDispatch4)sc;
            if (sd4 != null)
                sd4.ToggleDesktop();   //   Win+D   
            sc.MinimizeAll();

            sd4 = null;
            sc = null;
        }


    }
}
